﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CRM.DataAccess.Entities;
using CRM.Business.Services;
using CRM.Encryption;
using System.Data;
using Telerik.Web.UI;
using System.Configuration;

namespace Web.Account
{
    public partial class ChangePassword : System.Web.UI.Page
    {
        static string requestEdit = string.Empty;
        static USER user;
        static ROLE r;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
                return;
            user = (USER)Session["User"];
            if (user != null)
            {
                r = RoleService.GetRoleByID((long)user.ROLE_ID);
                if ((!(bool)user.IS_ADMIN) && (!(bool)r.IS_MANAGER))
                    return;
                requestEdit = Request.QueryString["UserID"];
            }
        }

        protected void btn_Save_ServerClick(object sender, EventArgs e)
        {
            if (requestEdit != null && requestEdit.Trim() != "")
            {
                USER newUser = new USER();
                newUser.ID = Convert.ToInt64(requestEdit);
                newUser.USER_HASH = RSAEncryption.PrivateEncryption(txtPassword.Text);
                int re = UserService.ChangePassword(newUser);
                if (re == 1)
                {
                    string script = "function f(){closeWin(); Sys.Application.remove_load(f);}Sys.Application.add_load(f);";
                    ScriptManager.RegisterStartupScript(Page, Page.GetType(), "key", script, true); 
                }
                else
                    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "", "alert('Can not change password !');", true);
            }
        }
    }
}